<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" v-auth="'client.add'" @click="add"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" v-auth="'client.delete'" :disabled="selection.length == 0" @click="batch_del"></el-button>
				<sc-export-button :auth="'client.export'" :apiObj="apiObj" :search="search"></sc-export-button>
				<!-- <el-button type="primary" plain v-auth="'goods.checkCate'" :disabled="selection.length == 0" @click="changeCate">批量修改分类</el-button> -->
				<!-- <sc-file-import :apiObj="$API.device.beiJian.goods.import" @success="success" ref="importBox"
				 :templateUrl="'http://2063.mzth.cn/static/goods_import.xls'" v-if="showUp"></sc-file-import> -->
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.customer_code" :placeholder="$t('sale.client.code')" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.customer_name" :placeholder="$t('sale.client.name')" clearable @keyup.enter="upsearch"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
				<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
				<el-table-column type="index" :label="$t('all.sort')" fixed align="center" width="60"></el-table-column>
				<el-table-column :label="$t('sale.client.code')" prop="customer_code" align="center"></el-table-column>
				<el-table-column :label="$t('sale.client.name')" prop="customer_name" align="center"></el-table-column>
				<el-table-column :label="$t('sale.client.source')" prop="sourcetxt.source_name" align="center"></el-table-column>
				<el-table-column :label="$t('sale.client.contacts')" prop="contacts" align="center"></el-table-column>
				<el-table-column :label="$t('sale.client.mobile')" prop="mobile" align="center"></el-table-column>
				<el-table-column :label="$t('sale.client.address')" prop="address" align="center"></el-table-column>
				<!-- <el-table-column label="当前应收款" prop="cate.cate_name" align="center"></el-table-column> -->
				<!-- <el-table-column label="期初应收款" prop="collect_price" align="center"></el-table-column> -->
                <el-table-column :label="$t('sale.client.payment')" prop="advance_price" align="center"></el-table-column>
				<el-table-column :label="$t('all.remark')" prop="remark" align="center"></el-table-column>
				<!-- <el-table-column :label="$t('all.status')" prop="weight" align="center"></el-table-column> -->
				<el-table-column :label="$t('all.status')" prop="system_disable" align="center" width="100">
					<template #default="scope">
                        <el-switch v-model="scope.row.system_disable" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="0" inline-prompt :active-text="$t('all.enable')" :inactive-value="1" :inactive-text="$t('all.disable')"></el-switch>
                    </template>
				</el-table-column>
				<el-table-column :label="$t('all.operate')" fixed="right" align="center" width="170">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'client.show'">{{$t('all.show')}}</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'client.edit'">{{$t('all.edit')}}</el-button>
							<el-popconfirm :title="$t('all.suredel')" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="danger" size="small" v-auth="'client.delete'">{{$t('all.delete')}}</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>

			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
</template>

<script>
import saveDialog from './save'

export default {
	name: '客户列表',
	components: {
		saveDialog,
	},
	data() {
		return {
			dialog: {
				save: false,
			},
			apiObj: this.$API.client.client.list,
			selection: [],
			search: {
				customer_name: null,
				customer_code: null,
			},
			applyList: [],
			pid: "",
		}
	},
	mounted() {
	},
	methods: {
		changeCate() {
			var ids = [];
			this.selection.forEach((item) => {
				ids.push(item.id)
			})
			this.dialog.cate = true
			this.$nextTick(() => {
				this.$refs.cateDialog.open('add').setData(ids)
			})
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add').getPid(this.pid)
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//表格内开关
		async changeSwitch(val, row) {
			row.$switch_status = true;
			var res = await this.$API.client.client.update_status.post({
				id: row.id,
				system_disable: row.system_disable,
			});
			if (res.code == 1) {
				row.$switch_status = false;
				this.$refs.table.refresh();
				this.$message.success(this.$t('all.yes'));
			} else {
				this.$alert(res.message, this.$t('all.prompt'), {type: "error"});
			}
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.client.client.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
				this.$refs.table.refresh()
				this.$message.success(this.$t('all.delYes'))
			} else {
				this.$alert(res.message,this.$t('all.prompt'), {type: "error"});
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.client.client.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success(this.$t('all.delYes'))
			} else {
				this.$alert(res.message,this.$t('all.prompt'), {type: "error"});
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`${this.$t('all.longmessage')} ${this.selection.length} ${this.$t('all.term')}`, this.$t('all.prompt'), {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		}
	}
}
</script>

<style></style>
